package com.xiaxiang.ar;

import android.Manifest;
import android.annotation.TargetApi;
import android.app.Activity;
import android.content.pm.PackageManager;
import android.os.Build;
import android.os.Bundle;
import android.support.annotation.NonNull;
import android.view.Menu;
import android.view.MenuItem;
import android.view.ViewGroup;
import android.view.WindowManager;
import android.util.Log;

import java.util.HashMap;

import cn.easyar.Engine;


public class TestActivity extends Activity
{
    private static String key = "02RM5LbW6kZ2vl5ANPLmcCpMu7VvLuywqf6u8r7GzRM4R5fboQbWbzlA9nHkdKAYA4844batc22cNr224C2dm8dtWHKboBXvrEv2C2MFI68t8rfIXTAIWU4b4IKFiEvbso2DmTblQEjdydtgvc8gminrM06L1KTHk1XB0Ndv5nuiumye22OucAWibB6BPGB8Hl0lDnxh";
    private static String cloud_server_address = "7051d01555d828a8087b615330002d00.cn1.crs.easyar.com:8080";
    private static String cloud_key = "5a2bf7398eb90bd9816995870a6feda5";
    private static String cloud_secret = "7zdjD1xQzEQWSlKQoxoqswBYaNLnKYKKmEGx0Li8nUQHsTVLlHPinH4LRUyKaArqDtZUNcBijgAUVDaJveyN9WmlIkHxZHEDmF0rAeSdNLZ3DywM5EfAGWVig63DjRPv";
    private GLView glView;

    @Override
    protected void onCreate(Bundle savedInstanceState)
    {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_test);
        Log.e("dir","dir is "+getApplicationContext().getApplicationInfo().nativeLibraryDir);
        getWindow().setFlags(WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON, WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON);

        if (!Engine.initialize(this, key,"/data/data/com.xiaxiang.xiawan/lib")) {
            Log.e("HelloAR", "Initialization Failed.");
        }

        glView = new GLView(this, cloud_server_address, cloud_key, cloud_secret);

        requestCameraPermission(new PermissionCallback() {
            @Override
            public void onSuccess() {
                ((ViewGroup) findViewById(R.id.preview)).addView(glView, new ViewGroup.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT));
            }

            @Override
            public void onFailure() {
            }
        });
    }

    private interface PermissionCallback
    {
        void onSuccess();
        void onFailure();
    }

    private HashMap<Integer, PermissionCallback> permissionCallbacks = new HashMap<Integer, PermissionCallback>();
    private int permissionRequestCodeSerial = 0;
    @TargetApi(23)
    private void requestCameraPermission(PermissionCallback callback)
    {
        if (Build.VERSION.SDK_INT >= 23) {
            if (checkSelfPermission(Manifest.permission.CAMERA) != PackageManager.PERMISSION_GRANTED) {
                int requestCode = permissionRequestCodeSerial;
                permissionRequestCodeSerial += 1;
                permissionCallbacks.put(requestCode, callback);
                requestPermissions(new String[]{Manifest.permission.CAMERA}, requestCode);
            } else {
                callback.onSuccess();
            }
        } else {
            callback.onSuccess();
        }
    }

    @Override
    public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults)
    {
        if (permissionCallbacks.containsKey(requestCode)) {
            PermissionCallback callback = permissionCallbacks.get(requestCode);
            permissionCallbacks.remove(requestCode);
            boolean executed = false;
            for (int result : grantResults) {
                if (result != PackageManager.PERMISSION_GRANTED) {
                    executed = true;
                    callback.onFailure();
                }
            }
            if (!executed) {
                callback.onSuccess();
            }
        }
        super.onRequestPermissionsResult(requestCode, permissions, grantResults);
    }

    @Override
    protected void onResume()
    {
        super.onResume();
        if (glView != null) { glView.onResume(); }
    }

    @Override
    protected void onPause()
    {
        if (glView != null) { glView.onPause(); }
        super.onPause();
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu)
    {
        getMenuInflater().inflate(R.menu.menu_main, menu);
        return true;
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item)
    {
        int id = item.getItemId();

        if (id == R.id.action_settings) {
            return true;
        }

        return super.onOptionsItemSelected(item);
    }
}
